package com.example.nio.nio.util;

import java.nio.ByteBuffer;

/**
 * @auther:lgb
 * @Date: 2021/5/3
 */
public class PacketWrapper {

    private int length;
    private byte[] payload;

    public PacketWrapper(String payload) {
        this.payload = payload.getBytes();
        this.length = this.payload.length;
    }

    public PacketWrapper(byte[] payload) {
        this.payload = payload;
        this.length = this.payload.length;
    }

    public byte[] getBytes() {
        ByteBuffer byteBuffer = ByteBuffer.allocate(this.length + 4);
        byteBuffer.putInt(this.length);
        System.out.println("客户端发送的长度为：" + this.length);
        byteBuffer.put(payload);
        return byteBuffer.array();
    }

}
